package avicit.bdp.dms.das.runner;

import avicit.bdp.common.utils.ConfigUtils;
import avicit.bdp.dms.das.job.BdpDasJob;
import avicit.bdp.dms.das.service.BdpDasStatService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.CommandLineRunner;
import org.springframework.context.annotation.Configuration;
import org.springframework.core.annotation.Order;

/**
 * 启动时判断昨天定时任务有没有正确执行
 *
 * @param
 * @author felix
 * @return
 * @date 2021/1/13 17:51
 */
@Configuration
@Order(1)
public class DasStartupRunner implements CommandLineRunner {

    private static final Logger log = LoggerFactory.getLogger(DasStartupRunner.class);

    @Autowired
    private BdpDasStatService bdpDasStatService;

    @Autowired
    private BdpDasJob bdpDasJob;

    @Override
    public void run(String... args) throws Exception {
        log.info(">>>>>>>>>>>>>>>服务启动执行，判断定时任务执行没有，没执行的话执行  <<<<<<<<<<<<<");
        // 初始化调度任务
        try {
            boolean isInitJob = ConfigUtils.getInstance().getBoolean("system", "isInitJob", true);
            log.info(">>>>>>>>>>>>>>>服务启动执行，判断定时任务执行没有，没执行的话执行  <<<<<<<<<<<<<"+isInitJob);
            if (isInitJob) {
                boolean isRun = bdpDasStatService.isRun();
                if (!isRun) {
                    runJob();
                }
            }
        } catch (Exception e) {
            log.error(">>>>>>>>>>>>>>>服务启动异常 <<<<<<<<<<<<<", e);
        }

    }


    /**
     * 从新执行定时任务
     *
     * @param
     * @return void
     * @author felix
     * @date 2021/1/13 17:46
     */
    private void runJob() {
        log.info("runJob-----------------------");
        try {
            bdpDasJob.statData();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

}
